package com.atzzy.ysmall.ysmall_orderserviceimpl;

import com.atzzy.ysmall.ysmall_allService.orderService.OrderService;
import com.atzzy.ysmall.ysmall_beans.OrderItem;
import com.atzzy.ysmall.ysmall_beans.Orders;
import com.atzzy.ysmall.ysmall_beans.ProductSku;
import com.atzzy.ysmall.ysmall_mapper.OrderItemMapper;
import com.atzzy.ysmall.ysmall_mapper.OrdersMapper;
import com.atzzy.ysmall.ysmall_mapper.ProductSkuMapper;
import com.github.wxpay.sdk.WXPay;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author JAVAZeng
 * @Date 2022/4/9 14:07
 * @Version 1.0
 */
@Component
public class OrderTimeoutCheckJob {

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
//    private OrderService orderService;
    private OrderItemMapper orderItemMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;

    @Scheduled(cron = "0/3 * * * * ?")
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void checkAndCloesOrder() {

        //查询超过30分钟订单状态依然未支付
        String status = "1";
        Date nowTime = new Date(System.currentTimeMillis() - 30 * 60 * 1000);
        List<Orders> orders = ordersMapper.selectOrdersLessThan(status, nowTime);

        MyPayConfig myPayConfig = new MyPayConfig();
        WXPay wxPay = new WXPay(myPayConfig);
        try {
            for (int i = 0; i < orders.size(); i++) {
                Orders order = orders.get(i);
                HashMap<String, String> params = new HashMap<>();
                params.put("out_trade_no", order.getOrderId());
                Map<String, String> resp = wxPay.orderQuery(params);

                if ("SUCCESS".equalsIgnoreCase(resp.get("trade_state"))) {
                    String statu = "2";
                    String orderId = order.getOrderId();
                    ordersMapper.updateStatus(orderId, statu);
                } else if ("NOTPAY".equalsIgnoreCase(resp.get("trade_state"))) {

                    Map<String, String> stringStringMap = wxPay.closeOrder(params);
                    System.out.println(stringStringMap);

                    synchronized (this) {
                        String orderId2 = order.getOrderId();
                        String status2 = "6";
                        int closeType = 1;
                        ordersMapper.updateStatusAndCloseTypeInt(orderId2, status2, closeType);


                        String orderItemId = order.getOrderId();
                        OrderItem orderItem = orderItemMapper.selectOrderItemById(orderItemId);
                        String skuId = orderItem.getSkuId();
                        ProductSku productSku = productSkuMapper.selectProSkuBySkuId(skuId);
                        int stock = (int) (productSku.getStock() + orderItem.getBuyCounts());
                        productSkuMapper.updateProductSkuNum(skuId, stock);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
